package com.vehicle.interceptor;

import com.vehicle.annotations.NoAuthCheck;
import com.vehicle.domains.entity.SysPermissionEntity;
import com.vehicle.domains.entity.SysRoleEntity;
import com.vehicle.domains.info.UserInfo;
import com.vehicle.enums.PermissionTypeEnum;
import com.vehicle.service.SysUserService;
import com.vehicle.threadLocal.UserContextThreadLocal;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.List;

@Component
public class PermissionInterceptor implements HandlerInterceptor {
    @Resource
    private SysUserService sysUserService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Integer userId = (Integer) request.getAttribute("userId");
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(userId));

        String uri = request.getRequestURI();

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        NoAuthCheck authCheck = handlerMethod.getMethodAnnotation(NoAuthCheck.class);
        if (authCheck != null||handlerMethod.getBeanType().getAnnotation(NoAuthCheck.class)!=null) {
            return true;
        }
        UserInfo userRoleAndPermission = sysUserService.getUserRoleAndPermission(Long.valueOf(userId));
        List<SysPermissionEntity> permissionEntityList = userRoleAndPermission.getPermissionEntityList();
        permissionEntityList = permissionEntityList.stream().filter(sysPermissionEntity -> PermissionTypeEnum.API.name().equals(sysPermissionEntity.getPermissionType())).toList();
        boolean flag = false;
        for (SysPermissionEntity sysPermissionEntity : permissionEntityList) {
            if (uri.equals(sysPermissionEntity.getPermissionCode())) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"code\":100011,\"message\":\"抱歉，您的级别还不够！！\"}");
            return false;
        }
        userInfo.setPermissionEntityList(permissionEntityList);
        List<SysRoleEntity> roleEntityList = userRoleAndPermission.getRoleEntityList();
        userInfo.setRoleEntityList(roleEntityList);
        UserContextThreadLocal.setUserInfo(userInfo);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserContextThreadLocal.remove();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
